const { exec } = require('child_process');
const FileHandler = require('./FileHandler');
const fileHandler = new FileHandler('../data');
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
// 你要在浏览器中打开的URL
const url = 'https://www.xiaohongshu.com/explore/';

puppeteer.use(StealthPlugin());
fileHandler.readFile('重庆旅游', (type, data) => {


  console.log(data)
  let data1 = ["65924e220000000010010927", "65e73a96000000000400045e"]
  if (data && Array.isArray(data1)) {
    data1.forEach((id, index) => {
      setTimeout(() => {
        // 根据操作系统选择适当的命令
        let openCommand;
        switch (process.platform) {
          case 'darwin': // macOS
            openCommand = `open ${url}` + id;
            break;
          case 'win32': // Windows
            openCommand = `start ${url}` + id;
            break;
          default: // Linux 和其他UNIX系统
            openCommand = `xdg-open ${url}` + id;
        }
        (async () => {
          const browser = await puppeteer.launch({
            headless: false,
            executablePath: 'C:\\Program Files\\Google\\Chrome Dev\\Application\\chrome.exe',
            args: [
              '--no-sandbox',
              '--disable-setuid-sandbox',
              '--disable-dev-shm-usage'
            ],
            ignoreDefaultArgs: ['--enable-automation', "--enable-automation"]
            // userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
          });

          const page = await browser.newPage();
          await page.evaluate(async () => {
            Object.defineProperty(navigator, 'webdriver', { get: () => false });
          });
          await page.evaluate(async () => {
            Object.defineProperty(navigator, 'chrome ', { get: () => { runtime: { } } });
          });
          // await page.page.evaluateOnNewDocument(() => {
          //   Object.defineProperty(navigator, 'webdriver', {
          //     get: () => undefined,
          //   });
          // })
          await page.setUserAgent(await page.browser().userAgent());
          // 打开一个页面
          await page.goto(url + id);

          // // 一段时间后关闭页面
          // await setTimeout(async () => {
          //   await browser.close();
          // }, 10000); // 5秒后关闭页面
        })();
        // 执行命令来打开浏览器
        // exec(openCommand, (err, stdout, stderr) => {
        //   if (err) {
        //     console.error(`无法打开浏览器：${err}`);
        //     return;
        //   }
        //   console.log('浏览器已打开！');
        // });
      }, index * 10000)
    })


  }


})

// (async () => {
//   const browser = await puppeteer.launch({ headless: false });
//   const page = await browser.newPage();
//   await page.goto('https://example.com');
//   // await page.screenshot({path: 'example.png'});

//   // await browser.close();
// })();